package com.bjsj.pro.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.bjsj.pro.tools.Configs;
import com.bjsj.pro.tools.GlobalTools;
import com.bjsj.pro.wxapi.WXLoginCode;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;



import de.greenrobot.event.EventBus;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {



    private IWXAPI api;
    protected GlobalTools tools;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Configs.WX_APP_ID);
        api.handleIntent(getIntent(), this);
        tools = GlobalTools.getInstance();
        //   System.out.println("=========onCreate WXEntryActivity");
    }

    /**
     * 微信发送请求到第三方应用时，会回调到该方法
     *
     * @param req 回调
     */
    @Override
    public void onReq(BaseReq req) {

        finish();
    }

    /**
     * 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
     *
     */



    @Override
    protected void onResume()
    {
        super.onResume();
        System.out.println("===========支付结果回调页面 WXEntryActivity=");
    }



    @Override
    public void onResp(BaseResp baseResp)
    {
        System.out.println("==========baseResp.errCode:"+baseResp.errCode);
        switch (baseResp.errCode)
        {

            case BaseResp.ErrCode.ERR_OK:
                System.out.println("==========baseResp.getType:"+baseResp.getType());
                switch (baseResp.getType())
                {

                    case ConstantsAPI.COMMAND_SENDAUTH:
                        //登录成功
                        System.out.println("==========微信登录成功");
                        String code = ((SendAuth.Resp) baseResp).code;
                        EventBus.getDefault().post(new WXLoginCode(code,"303774"));
                        break;
                    case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
                        //  Toast.makeText(getApplicationContext(), "分享成功", Toast.LENGTH_SHORT).show();
                        break;

                    case ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM:
                        //打开小程序后的回调ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM
                        System.out.println("==========COMMAND_LAUNCH_WX_MINIPROGRAM 打开小程序后的回调");
                      /*  LjyhApp.getAppInstance().exitApp();
                        startActivity(new Intent(this, MainActivity.class));
                        EventBus.getDefault().post(new UpdateShopingCar(0));
                        EventBus.getDefault().post(new UpdatePersonal(true));
                        IntentTools.startOrderActivity(this, 0);*/
                        break;

                }
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED://用户拒绝授权
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL://用户取消
                finish();
                break;
            default:
                finish();
                break;
        }

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    /*@Override
    public void onResp(BaseResp baseResp) {
        //2018/8/29 App调起小程序
        if(baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            WXLaunchMiniProgram.Resp launchMiniProResp = (WXLaunchMiniProgram.Resp) baseResp;
            String extraData =launchMiniProResp.extMsg; //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
        }
    }*/

}
